<template>
    <div class="box">
        <div class="main">
            <div class="top">
                <h3>手机号登录</h3>
            </div>
            <div class="middle">
                <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">
                    <el-form-item label="用户" prop="username">
                        <el-input v-model="ruleForm.username" />
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input v-model="ruleForm.password" />
                    </el-form-item>
                    <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
                    <el-button @click="resetForm(ruleFormRef)">重置</el-button>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script setup>
import { reactive, ref } from 'vue'

import { ElMessage } from 'element-plus'

import instance from '@/request/request'

import { useRoute, useRouter } from 'vue-router'
const router = useRouter()

const ruleFormRef = ref()
const ruleForm = reactive({
    username: '',
    password: '',
})

const rules = reactive({
    username: [
        { pattern: /^1[3-9]\d{9}$/, required: true, message: '请输入用户名称', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 3, max: 8, message: '请输入正确的密码', trigger: 'blur' },
    ],
})

const submitForm = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log('submit!', ruleForm)
            instance.post('login', ruleForm).then((res) => {
                console.log(res)
                if (res.data.code == 10000) {
                    ElMessage({
                        message: '登录成功！',
                        type: 'success',
                    })
                    localStorage.setItem('accessToken', res.data.data.accessToken)
                    localStorage.setItem('refreshToken', res.data.data.refreshToken)
                    localStorage.setItem('avator', res.data.data.userInfo.avator)
                    localStorage.setItem('nickname', res.data.data.userInfo.nickname)
                } else {
                    ElMessage.error(res.data.message)
                }
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
}

</script>

<style lang="scss" scoped>
.box {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;

    .main {
        width: 400px;
        height: 300px;
        border: 1px solid #000;

        .top {
            width: 100%;
            height: 40px;
            // background: #cfc;
            text-align: center;
        }

        .middle {
            .el-form {
                margin-left: 5%;

                .el-input {
                    width: 90%;
                }
            }
        }
    }
}
</style>